package com.leilei.greendao.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.leilei.greendao.Myapplication;
import com.leilei.greendao.greendao.DaoMaster;
import com.leilei.greendao.greendao.DaoSession;
import com.leilei.greendao.mode.Person;

import java.util.Date;
import java.util.List;

/**
 * Created by leilei on 2018/1/27.
 */

public class GreenDaoManager {
    private static GreenDaoHelp mHelper;
    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    private static String PASS_WORD="1234";

    /**
     * 对外唯一实例的接口
     *
     * @return
     */
    public static GreenDaoManager getInstances() {
        return SingleInstanceHolder.INSTANCE;
    }

    /**
     * 静态内部类，实例化对象使用
     */
    private static class SingleInstanceHolder {
        private static final GreenDaoManager INSTANCE = new GreenDaoManager();
    }

    private GreenDaoManager() {
        if (mHelper == null) {
            mHelper = new GreenDaoHelp(Myapplication.getInstances());
        }
        getDaoMaster();
        getDaoSession();
    }

    /**
     * 增
     */
    public void insert() {
        for (int i = 0; i < 1000; i++) {
            String date = new Date().toString();
            Person person = new Person(null, date, 0, "");
            getDaoSession().insertOrReplace(person);
        }
    }

    /**
     * 查
     */
    public List<Person> query() {
        List<Person> mList = getDaoSession().getPersonDao().loadAll();
        return mList;
    }


    /**
     * 改
     */
    public void update(long i, String date) {
//        dayStep mDayStep = new dayStep(null, date, 0);
//        getDaoSession().getDayStepDao().insertOrReplace(mDayStep);
    }

    /**
     * 删
     *
     * @param i
     */
    public void delete(long i) {
        getDaoSession().getPersonDao().deleteByKey(i);
    }

    /**
     * 获取可读数据库
     *
     * @param context
     * @return
     */
    public static SQLiteDatabase getReadableDatabase(Context context) {
        if (null == mHelper) {
            getInstances();
        }
        return mHelper.getReadableDatabase();
    }

    /**
     * 获取可写数据库
     *
     * @param context
     * @return
     */
    public static SQLiteDatabase getWritableDatabase(Context context) {
        if (mHelper == null) {
            getInstances();
        }
        return mHelper.getWritableDatabase();
    }

    /**
     * 获取DaoMaster
     * <p>
     * 判断是否存在数据库，如果没有则创建数据库
     */
    public static DaoMaster getDaoMaster() {
        if (mDaoMaster == null) {
            synchronized (GreenDaoManager.class) {
                if (mDaoMaster == null) {
                    //升级对应的代码，请关注下面的一个类
                    mDaoMaster = new DaoMaster(mHelper.getWritableDatabase());
                    //数据库加密
//                    mDaoMaster = new DaoMaster(mHelper.getEncryptedReadableDb(PASS_WORD));
                }
            }
        }
        return mDaoMaster;
    }

    /**
     * 获取DaoSession
     */
    public static DaoSession getDaoSession() {
        if (mDaoSession == null) {
            synchronized (GreenDaoManager.class) {
                mDaoSession = getDaoMaster().newSession();
            }
        }
        return mDaoSession;
    }

}
